import { createRouter, createWebHistory,NavigationGuardNext,RouteLocationNormalized,RouteLocationNormalizedLoaded } from 'vue-router'
import route from './route'
import {useMenus}from "@/stores"
const router = createRouter({
  history: createWebHistory(),
  routes:[
    {
      path: '/',
      redirect: '/home'
    },
    {
      // 任意路由
      path: '/:catchAll(.*)*',
      component:()=>import('@/views/errorPage/404.vue'),
      name: '404',
      meta:{
          title:'~页面丢失了~'
      }
    },
    ...route
  ],
  scrollBehavior() {
    return { top: 0 }
  }
})

router.beforeEach((to:RouteLocationNormalized, from:RouteLocationNormalizedLoaded, next:NavigationGuardNext)=>{
    const menu = useMenus()
    document.title=to?.meta?.title as string
    menu.setRouter()
    if(to.path != menu.getactiveRouter){
      next(menu.getactiveRouter)
      return
    }
    next()
})

export default router
